﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Domain.PlcBytesObject
{
    /// <summary>
    /// 成品库分档组盘位写DB报文
    /// </summary>
    public class PlanarZuPanWriteWcsAction : WriteByteObjectBase
    {
        public PlanarZuPanWriteWcsAction() { }
        public PlanarZuPanWriteWcsAction(long plcConnectionID
            , long plcWriteDbID
            , int dbCode
            , int dbStart
            , string objectType
            , int equipmentCode
            , short actionType
            , int toEquipmentCode
            , int taskCode
            , string palletBarcode
            , byte palletType
            , byte alrmCode
            , byte reserve = 0
            , byte reserve2 = 0
            , byte reserve3 = 0
            , byte reserve4 = 0)
        {
            PlcConnectionID = plcConnectionID;
            PlcWriteDbID = plcWriteDbID;
            DbCode = dbCode;
            DbStart = dbStart;
            ObjectType = objectType;
            EquipmentCode = equipmentCode;
        }


        ///// <summary>
        ///// 任务号
        ///// </summary>
        //public int TaskCode { get; set; }
        ///// <summary>
        ///// 目标设备号
        ///// </summary>
        //public int ToEquipmentCode { get; set; }
        ///// <summary>
        ///// 托盘码
        ///// </summary>
        //public string PalletBarcode { get; set; }
        ///// <summary>
        ///// 握手类型
        ///// </summary>
        //public short ActionType { get; set; }
        ///// <summary>
        ///// 托盘类型
        ///// </summary>
        //public byte PalletType { get; set; }
        ///// <summary>
        ///// 报警代码
        ///// </summary>
        //public byte AlrmCode { get; set; }

        ///// <summary>
        ///// 单机任务标识
        ///// </summary>
        //public byte Reserve { get; set; }
        ///// <summary>
        ///// 预留2
        ///// </summary>
        //public byte Reserve2 { get; set; }
        ///// <summary>
        ///// 预留3
        ///// </summary>
        //public byte Reserve3 { get; set; }
        ///// <summary>
        ///// 预留4
        ///// </summary>
        //public byte Reserve4 { get; set; }

        ///// <summary>
        ///// 组盘数据创建申请
        ///// </summary>
        //public byte ZupanRequest { get; set; }

        ///// <summary>
        ///// 组盘数据申请响应
        ///// </summary>
        //public byte ZupanResponse { get; set; }

        ///// <summary>
        ///// 预留-0
        ///// </summary>
        //public byte Reserve_0 { get; set; }

        ///// <summary>
        ///// 预留-1
        ///// </summary>
        //public byte Reserve_1 { get; set; }

        ///// <summary>
        ///// 预留-2
        ///// </summary>
        //public byte Reserve_2 { get; set; }

        ///// <summary>
        ///// 预留-3
        ///// </summary>
        //public byte Reserve_3 { get; set; }

        #region 电池信息
        
        /// <summary>
        /// 电池集合
        /// </summary>
        public List<ZupanBatteryItemDto> BatteryItems { get; set; }

        #endregion


        public override byte[] Deserialize()
        {
            byte[] bytes = new byte[BatteryItems.Count * 36];
            //HelperByte.SetDIntAt(bytes, 0, EquipmentCode);
            //HelperByte.SetDIntAt(bytes, 4, TaskCode);
            //HelperByte.SetDIntAt(bytes, 8, ToEquipmentCode);
            //HelperByte.SetByteAt(bytes, 12, 18);

            //byte length = Convert.ToByte(PalletBarcode.Length);
            //HelperByte.SetByteAt(bytes, 13, length);
            //HelperByte.SetCharsAt(bytes, 14, PalletBarcode);

            //HelperByte.SetIntAt(bytes, 32, ActionType);
            //HelperByte.SetByteAt(bytes, 34, PalletType);
            //HelperByte.SetByteAt(bytes, 35, AlrmCode);
            //HelperByte.SetByteAt(bytes, 36, Reserve);
            //HelperByte.SetByteAt(bytes, 37, Reserve2);
            //HelperByte.SetByteAt(bytes, 38, Reserve3);
            //HelperByte.SetByteAt(bytes, 39, Reserve4);

            //HelperByte.SetByteAt(bytes, 0, ZupanRequest);
            //HelperByte.SetByteAt(bytes, 1, ZupanResponse);
            //HelperByte.SetByteAt(bytes, 2, Reserve_0);
            //HelperByte.SetByteAt(bytes, 3, Reserve_1);
            //HelperByte.SetByteAt(bytes, 4, Reserve_2);
            //HelperByte.SetByteAt(bytes, 5, Reserve_3);


            //电池信息集合 TODO测试OK则使用此方案
            for (int i = 0; i < BatteryItems.Count; i++)
            {
                //写入预留长度
                HelperByte.SetByteAt(bytes, 36 * i, 30);
                //写入电芯条码
                byte lengthItem = Convert.ToByte(BatteryItems[i].BatteryCode.Length);
                HelperByte.SetByteAt(bytes, 1 + 36 * i, lengthItem);
                HelperByte.SetCharsAt(bytes, 2 + 36 * i, BatteryItems[i].BatteryCode);
                //写入电芯档位
                HelperByte.SetIntAt(bytes, 32 + 36 * i, BatteryItems[i].BatteryType);
                //写入电芯类型
                HelperByte.SetByteAt(bytes, 34 + 36 * i, BatteryItems[i].BatteryGear);
            }

            return bytes;
        }

    }
}
